package ru.amse.baltijsky.javascheme.nodeshape;

import ru.amse.baltijsky.javascheme.util.Size;
import ru.amse.baltijsky.javascheme.util.VisConst;

import java.awt.*;

public class SynchronizedNodeShape extends AbstractNodeShape {
    /**
     * Text offset inside the text frame.
     */
    private static final Size OFFSET = new Size(5, 5);
    private static final int ARC_W = 8;
    private static final int ARC_H = 8;

    /**
     * Internal constructor, used to encapsulate the work with the code string and calculate node dimensions.
     *
     * @param code the code of the node
     */
    public SynchronizedNodeShape(String code) {
        super(code);
    }

    public void draw(int x, int y, Graphics g) {
        Size size = getBoundRectSize(g);
        int halfX = size.x / 2;
        int halfY = size.y / 2;

        Color oldColor = g.getColor();

        g.setColor(VisConst.SCHEME_COL);
        g.drawRoundRect(x - halfX, y - halfY, size.x, size.y, ARC_W, ARC_H);
        g.setColor(VisConst.TEXT_COL);
        g.drawString(getCode(), x - halfX + OFFSET.x, y + OFFSET.y);

        g.setColor(oldColor);
    }

    public Size getBoundRectSize(Graphics g) {
        Size codeSize = getCodeSize(g);
        return new Size(OFFSET.x + codeSize.x + OFFSET.x, OFFSET.y + codeSize.y + OFFSET.y);
    }
}
